//
//  MQEmoticonCollectionView.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/2/3.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
let MQEmoticonCollectionViewDelegateCell = "MQEmoticonCollectionViewDelegateCell"

class MQEmoticonCollectionView: UICollectionView {
    var choiceToolBarButton:((indexpath:NSIndexPath)->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
//        backgroundColor = UIColor.blueColor()
        self.dataSource = self
        self.delegate = self
        registerClass(MQEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: MQEmoticonCollectionViewDelegateCell)
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        self.pagingEnabled = true
        showsHorizontalScrollIndicator = false
        bounces = false
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(SCREENW, self.bounds.height)
    }
}

extension MQEmoticonCollectionView:UICollectionViewDataSource, UICollectionViewDelegate{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return MQEmoticonTools.shareTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return MQEmoticonTools.shareTools.allEmoticons[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(MQEmoticonCollectionViewDelegateCell, forIndexPath: indexPath) as! MQEmoticonCollectionViewCell
        cell.backgroundColor = UIColor.whiteColor()
        cell.indexPath = indexPath
        cell.emoticons = MQEmoticonTools.shareTools.allEmoticons[indexPath.section][indexPath.item]
        return cell
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //去到可见cell
        let cells = self.visibleCells()
        if cells.count == 2{//这句判断很重要！
            
            let firstCell = cells.first!
            let secondCell = cells.last!
            
            let firstCellX = abs(Int32((firstCell.frame.origin.x) - scrollView.contentOffset.x))
            let secondCellX = abs(Int32((secondCell.frame.origin.x) - scrollView.contentOffset.x))
            
            var indexpath = NSIndexPath()
            if firstCellX < secondCellX{//这里只是两个可见的cell，根据cell获取section
                indexpath = self.indexPathForCell(firstCell)!
            }else{
                indexpath = self.indexPathForCell(secondCell)!
            }
            // printfLog(section)
            choiceToolBarButton?(indexpath:indexpath)
        
        }
    }
}
